package cn.bytets.speed.provider;

import cn.bytest.netty.NettyTransporter;
import cn.bytets.rpc.api.RpcServer;
import cn.bytets.rpc.api.Transporters;
import cn.bytets.rpc.api.context.RpcAppContext;
import cn.bytets.speed.bean.impl.DemoServerImpl;

public class RpcServiceConfig<T> {

    /**
     * The interface name of the exported service
     */
    private String interfaceName;

    /**
     * The interface class of the exported service
     */
    private Class<T> interfaceClass;

    /**
     * The reference of the interface implementation
     */
    private Class ref;

    private volatile boolean init = false;

    private RpcServer bind;

    private volatile boolean start = false;

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public Class<?> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<T> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public Class getRef() {
        return ref;
    }

    public void setRef(Class ref) {
        this.ref = ref;
    }

    public synchronized void initConfig(){
        if (init){
            return;
        }
        try {
            Object instance = ref.newInstance();
            RpcAppContext.getInstance().addBean(this.interfaceClass.getName(),instance);
            this.bind = Transporters.getInstance().setTransporters(new NettyTransporter()).bind();
            this.init = true;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public synchronized void start(){
        if (start){
            return;
        }
        this.bind.start();
        start = true;
    }
}
